package com.copplay.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.copplay.common.core.utils.MapstructUtils;
import com.copplay.common.core.utils.ObjectUtils;
import com.copplay.common.core.utils.StringUtils;
import com.copplay.common.mybatis.core.page.PageQuery;
import com.copplay.common.mybatis.core.page.TableDataInfo;
import com.copplay.system.domain.Notice;
import com.copplay.system.domain.User;
import com.copplay.system.web.bo.NoticeBo;
import com.copplay.system.web.vo.NoticeVo;
import com.copplay.system.web.vo.UserVo;
import com.copplay.system.mapper.NoticeMapper;
import com.copplay.system.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl implements SysNoticeService {

    private final NoticeMapper baseMapper;
    private final UserMapper userMapper;

    @Override
    public TableDataInfo<NoticeVo> selectPageNoticeList(NoticeBo notice, PageQuery pageQuery) {
        LambdaQueryWrapper<Notice> lqw = buildQueryWrapper(notice);
        Page<NoticeVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public NoticeVo selectNoticeById(Long noticeId) {
        return baseMapper.selectVoById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<NoticeVo> selectNoticeList(NoticeBo notice) {
        LambdaQueryWrapper<Notice> lqw = buildQueryWrapper(notice);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Notice> buildQueryWrapper(NoticeBo bo) {
        LambdaQueryWrapper<Notice> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNoticeTitle()), Notice::getNoticeTitle, bo.getNoticeTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeType()), Notice::getNoticeType, bo.getNoticeType());
        if (StringUtils.isNotBlank(bo.getCreateByName())) {
            UserVo sysUser = userMapper.selectVoOne(new LambdaQueryWrapper<User>().eq(User::getUserName, bo.getCreateByName()));
            lqw.eq(Notice::getCreateBy, ObjectUtils.notNullGetter(sysUser, UserVo::getUserId));
        }
        lqw.orderByAsc(Notice::getNoticeId);
        return lqw;
    }

    /**
     * 新增公告
     *
     * @param bo 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(NoticeBo bo) {
        Notice notice = MapstructUtils.convert(bo, Notice.class);
        return baseMapper.insert(notice);
    }

    /**
     * 修改公告
     *
     * @param bo 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(NoticeBo bo) {
        Notice notice = MapstructUtils.convert(bo, Notice.class);
        return baseMapper.updateById(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return baseMapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return baseMapper.deleteByIds(Arrays.asList(noticeIds));
    }
}
